
<div class="layui-card-header">指标管理</div>
<div class="layui-card-body">
    <!-- 更多筛选条件弹窗 -->
    <div class="" id="idenManagePageMoreFilterModel">
        <fieldset class="layui-elem-field layui-field-title" >
            <legend>更多筛选</legend>
        </fieldset>
        <form id="idenManageMoreFilterForm" lay-filter="idenManageMoreFilterForm" class="layui-form model-form ">
            <div class="layui-form-item">
                <label class="layui-form-label">指标名称</label>
                <div class="layui-input-block">
                    <input name="iden_name" placeholder="请输入用户名" type="text" class="layui-input" maxlength="20"
                           lay-verType="tips" lay-verify="" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">指标数据类型</label>
                <div class="layui-input-block">
                    <select name="iden_type" placeholder="请选择请求方法" id="search-iden-type">
                    </select>
                </div>
            </div>
            <div class="layui-form-item text-left">
                <button class="layui-btn layui-btn-primary layui-btn-sm" type="button" id="idenManageCloseMoreFilterModel">取消</button>
                <button class="layui-btn layui-btn-sm" lay-filter="iden-type_name-search" lay-submit >搜索</button>
            </div>
        </form>
    </div>
    <div id="idenManage-tmpl-box">

    </div>
    <div id="idenManageTablePageBox"></div>

    <table class="layui-table" id="idenTable" lay-filter="idenTable" lay-skin="nob"></table>
</div>

<!-- 分组表格操作列DOM -->
<script id="ident-idenManage-tpl-box">
</script>
<!-- 表格操作列 -->
<script type="text/tmpl" id="ident-idenManage-tmp">
    {{ if hasPower("/micro/microMeta/idenManage:edit")}}
    <a class="layui-btn layui-btn-xs layui-btn-warning" lay-event="edit">编辑</a>
    {{/if}}
    {{ if hasPower("/micro/microMeta/idenManage:delete")}}
    <a class="layui-btn layui-btn-danger layui-btn-xs " lay-event="delete">删除</a>
    {{/if}}
</script>
<!--渲染指标管理权限-->
<script type="text/tmpl" id="idenManage-tmpl">
    <div class="layui-form toolbar">
        <div class="layui-form-item">
            <div class="layui-inline">
                {{ if hasPower("/micro/microMeta/idenManage:queryList")}}
                <div class="layui-input-inline mr0">
                    <input name="idenName" class="layui-input layui-input-sm" type="text" placeholder="输入关键字"/>
                </div>
                {{/if}}
            </div>
            <div class="layui-inline">
                {{ if hasPower("/micro/microMeta/idenManage:queryList")}}
                <button class="layui-btn icon-btn layui-btn-sm" lay-filter="idenManageSearch" lay-submit>
                    <i class="layui-icon">&#xe615;</i>搜索
                </button>
                {{/if}}
                {{ if hasPower("/micro/microMeta/idenManage:queryList")}}
                <button class="layui-btn icon-btn layui-btn-sm" id="idenManagePage-morefilter" >
                    …   更多筛选
                </button>
                {{/if}}
                {{ if hasPower("/micro/microMeta/idenManage:add")}}
                <button class="layui-btn icon-btn layui-btn-sm" id="addIdenBtn" >
                    <i class="layui-icon">&#xe654;</i>新增
                </button>
                {{/if}}
                {{ if hasPower("/micro/microMeta/idenManage:import")}}
                <button class="layui-btn icon-btn layui-btn-sm" id="importIdenBtn" >
                    <i class="layui-icon">&#xe62f;</i>导入
                </button>
                {{/if}}
                 {{ if hasPower("/micro/microMeta/idenManage:copy")}}
                <button class="layui-btn icon-btn layui-btn-sm" id="copyIdenBtn" >
                    <i class="layui-icon">&#xe655;</i>复制
                </button>
                {{/if}}
            </div>
        </div>
    </div>
  </script>
<!--渲染指标数据类型-->
<script type="text/tmpl" id="template-iden_type-tmpl">
    <option value="">全部</option>
     {{each idenType}}
        <option value="{{$value.value}}">{{$value.iden_type}}</option>
    {{/each}}

</script>
<script>
    // var powerlist = ["/micro/ident-group:queryList","/micro/ident-group:add"];
    <!--自定义函数判断权限-->
    var userInfo = getAuthority();
    var powerlist = JSON.parse(userInfo).permissions;
    /*自定义函数判断权限*/
    template.defaults.imports.hasPower = function(data){
        if(powerlist.includes(data)){
            return true;
        }
        return false;
    };
    layui.use(['layer', 'form', 'admin','table','laypage'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var table = layui.table;
        var laypage = layui.laypage;
        var fistRenderPage = true;
        var count = 0;//总个数
        var current=1;//当前页
        var iden_name='';//指标名称
        var iden_type='';//指标数据类型
        var  procurement = {};//流入渠道
        var useType = {};//流出方式
        var idenType = {};//指标类型容器
        var Authorization;
        var urlData = window.mSelUser;  // 列表页面传递的数据，也可以使用top.xxx
        //获取权限方法
        if(userInfo){
            var access_token = localStorage.getExpire("access_token");
            var token_type = localStorage.getExpire("token_type");
            Authorization = token_type+" "+access_token;
            getTableData({});
            getMicroIdenType();
            getProcurement();
            getUseType();
        }

        getTemplateIdenPower();
        getIdenManageTablePower();


        // 表格工具条点击事件
        table.on('tool(idenTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditIdenModel(data);
            } else if (layEvent === 'delete') { // 删除
                showDeleteIdenModel(data);
            }
        });
        $(".layui-layer-content").click(function(node){
            if(!(document.getElementById("idenManagePageMoreFilterModel").contains(node.target))){
                $("#idenManagePageMoreFilterModel").slideUp();
            }
        });
        // 指标管理搜索按钮点击事件
        form.on('submit(idenManageSearch)', function (data) {
//            let other_data = {
//                iden_name: data.field.idenName
//            }
            iden_name = data.field.idenName;
            iden_type = '';
            getTableData()
        });
        //更多筛选点击
        $('#idenManagePage-morefilter').click(function () {
            openMoreFilterModel();
            return false;
        })
        // 指标管理更多筛选
        form.on('submit(idenManageMoreFilterForm)', function (data) {
            $("#idenManagePageMoreFilterModel").slideUp();
            return false;
        });
        //筛选后的搜索
        form.on('submit(iden-type_name-search)', function (data) {
//            let other_data = {
//                iden_name: data.field.iden_name,
//                iden_type:data.field.iden_type
//            }
            iden_name = data.field.idenName;
            iden_type = data.field.iden_type;
            getTableData()
            $("#idenManagePageMoreFilterModel").slideUp();
            return false;
        });
        $("#idenManageCloseMoreFilterModel").click(function () {
            $("#idenManagePageMoreFilterModel").slideUp();
        });

        $("#addIdenBtn").click(function () {
            showAddIdenModel();
        });
        $("#importIdenBtn").click(function () {
            showImportIdenModel();
        });
        $("#copyIdenBtn").click(function () {
            showCopyIdenModel();
        })
        //获取表格数据
        function getTableData() {
            var req={
                current: current,
                size: pageSize,
                iden_name:iden_name,
                iden_type:iden_type,
                miid:mSelUser.miid
            };
            axios.get(microUrl.microObject.idenMicroMeta.idenMicroList,{
                params: req,
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (response) {
                    if(response.status == 200 &&response.data.message == "success"){
                        var data = response.data.data;
                        renderTable(data)
                    }
                })
                .catch(function (error) {
                    console.log(error)
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                });
        }
        function renderTable(tableData) {
            table.render({
                elem: '#idenTable',
                size: 'sm', //小尺寸的表格
                // skin:'nob',
                data:tableData.records,
                page: false,
                cellMinWidth: 100,
                cols: [[
                    {type:'numbers', title: '#'},
                    {field: 'idenName', align: 'center', title: '指标名称'},
                    {field: 'idenType', align: 'center', title: '指标数据类型',templet:function(d){
                        for(let i in idenType){
                            if(idenType[i].value == d.idenType ){
                                return idenType[i].iden_type;
                            }
                        }
                    }},
                    {field: 'idenSimpleName', align: 'center', title: '指标简称'},
                    {field: 'isDisplayed', align: 'center', title: '是否展示',templet:function (d) {
                            return d.isDisplayed?"是":"否"
                        }},
                    {field: 'isPk', align: 'center', title: '是否业务主键',templet:function (d) {
                            return d.isPk?"是":"否"
                        }},
                    {field: 'isSearch', align: 'center', title: '是否可搜索',templet:function (d) {
                            return d.isSearch?"是":"否"
                        }},
                    {field: 'status', align: 'center', title: '是否可用',templet:function (d) {
                            return d.status?"是":"否"
                        }},
                    {field: 'memo', align: 'center', title: '描述'},
                    {align: 'center', toolbar: '#ident-idenManage-tpl-box', title: '操作', minWidth: 400}
                ]]
            });
            //第一次才渲染分页
            if(fistRenderPage) {
                laypage.render({
                    elem: 'idenManageTablePageBox'
                    , count: tableData.total,
                    limit: tableData.size,
                    groups: 1,
                    current: current
                    , layout: ['count', 'prev', 'page', 'next']
                    , jump: function (obj, first) {
                        if (!first) {
                            current = obj.curr;
                            fistRenderPage = false;
                            getTableData({});
                        }
                    }
                });
            }
        }
        //获取指标数据类型
        function getMicroIdenType() {
            axios.get(microUrl.microObject.idenMicroMeta.idenMicroIdenType,{
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (response) {
                    if(response.status == 200 &&response.data.message == "success"){
                        var data = response.data.data;
                        idenType = data
                    }
                })
                .catch(function (error) {
                    console.log(error)
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                });
        }
        //获取流入渠道
        function getProcurement() {
            axios.get(microUrl.microObject.idenMicroMeta.idenProcurementType,{
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (response) {
                    if(response.status == 200 &&response.data.message == "success"){
                        var data = response.data.data;
                        procurement = data
                    }
                })
                .catch(function (error) {
                    console.log(error)
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                });
        }
        //获取流出方式
        function getUseType() {
            axios.get(microUrl.microObject.idenMicroMeta.idenUseType,{
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (response) {
                    if(response.status == 200 &&response.data.message == "success"){
                        var data = response.data.data;
                        useType = data
                    }
                })
                .catch(function (error) {
                    console.log(error)
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                });
        }
        /*渲染指标管理权限*/
        function getTemplateIdenPower() {
            var str = template("idenManage-tmpl",{});
            document.getElementById("idenManage-tmpl-box").innerHTML = str;
        }

        /*显示更多筛选框*/
        function openMoreFilterModel() {
            $("#idenManagePageMoreFilterModel").slideDown();
            var str = template("template-iden_type-tmpl",{idenType});
            document.getElementById("search-iden-type").innerHTML = str;
            form.render('select');
        }
        
        /*渲染分组表格里的权限*/
        function getIdenManageTablePower() {
            var str = template("ident-idenManage-tmp",{});
            document.getElementById("ident-idenManage-tpl-box").innerHTML = str;
        }

        /*显示新增指标弹窗*/
        function showAddIdenModel() {
            window.publicData = {
                useType:useType,
                procurement:procurement,
                idenType:idenType,
                id:mSelUser.miid
            };  // 传递数据到表单页面，也可以使用top.xxx
            admin.open({
                title: '新增指标',
                url: 'idenManage/addIden.html',
                area:['360px','750px'],
                end: function () {
                    fistRenderPage = true
                    getTableData({})
                },
                success: function (layero, dIndex) {
                    // 弹窗超出范围不出现滚动条
//                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /*显示导入指标弹窗*/
        function showImportIdenModel() {
            admin.open({
                title: '导入指标',
                url: 'idenManage/importIden.html',
                end: function () {

                },
                success: function (layero, dIndex) {
                    // 弹窗超出范围不出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /*显示复制指标弹窗*/
        function showCopyIdenModel() {
            admin.open({
                title: '复制指标',
                url: 'idenManage/copyIden.html',
                area:['500px','400px'],
                end: function () {

                },
                success: function (layero, dIndex) {
                    // 弹窗超出范围不出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /*显示编辑指标弹窗*/
        function showEditIdenModel(data){
            window.publicData = {
                useType:useType,
                procurement:procurement,
                idenType:idenType,
                idenData:data,
                id:mSelUser.miid
            };  // 传递数据到表单页面，也可以使用top.xxx
            admin.open({
                title: '编辑指标',
                url: 'idenManage/editIden.html',
                area:['360px','750px'],
                end: function () {
                    getTableData({})
                },
                success: function (layero, dIndex) {
                    // 弹窗超出范围不出现滚动条
//                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /*显示编辑指标弹窗*/
        function showDeleteIdenModel(data){
            layer.confirm('确定要删除指标：“'+data.idenName+ '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                axios({
                    method: 'delete',
                    url:microUrl.microObject.idenMicroMeta.deleteIdenMicroList + data.id,
                    headers:{
                        'Authorization':Authorization
                    }
                })
                    .then(function (res) {
                        if (res.status == 200&&res.data.message=="success") {
                            fistRenderPage = true
                            layer.closeAll('loading');
                            layer.msg("删除成功", {icon: 1});
                            getTableData({});
                        } else {
                            layer.msg(res.data.message, {icon: 2});
                            layer.closeAll('loading');
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                        if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                            // 进行重新登陆
                            localStorage.clear();
                            top.location.href=loginPageUrl;
                        }
                    })
            });
        }
    });
</script>

